import {
  createCmdConfig,
  DisposableCollection,
  XFlowGraphCommands
} from '@antv/xflow'
import type {
  NsNodeCmd,
  NsEdgeCmd,
  NsGraphCmd,
  IApplication,
  IGraphPipelineCommand
} from '@antv/xflow'
import { XFlowNode, XFlowEdge } from '../utils'

import { uuidv4, NsGraph, NsGraphStatusCommand } from '@antv/xflow'
import { NODE_TYPE_RENDER_KEY } from '@/constants'

export const useCmdConfig = createCmdConfig(config => {
  // 注册全局Command扩展
  // config.setCommandContributions(() => commandContributions)
  // 设置hook
  config.setRegisterHookFn(hooks => {
    const list = [
      // hooks.graphMeta.registerHook({
      //   name: 'get graph meta from backend',
      //   handler: async args => {
      //     args.graphMetaService = MockApi.queryGraphMeta
      //   },
      // }),
      // hooks.saveGraphData.registerHook({
      //   name: 'save graph data',
      //   handler: async args => {
      //     console.log(args);
          
      //     if (!args.saveGraphDataService) {
      //       args.saveGraphDataService = MockApi.saveGraphData
      //     }
      //   },
      // }),
      hooks.addNode.registerHook({
        name: 'get node config from backend api',
        handler: async args => {
          console.log('添加节点', args.nodeConfig)
          args.nodeConfig = {
            ...args.nodeConfig,
            renderKey: Object.values(NODE_TYPE_RENDER_KEY).includes(args.nodeConfig.renderKey) ? args.nodeConfig.renderKey : 'AUTO_BAN'
          }
          // if (args.nodeConfig.data.data) {
          //   console.log(123456,args.nodeConfig.data);
            
          //   args.nodeConfig.data = JSON.parse(JSON.stringify(args.nodeConfig.data.data))
          // }
        }
      }),
      // hooks.delNode.registerHook({
      //   name: 'get edge config from backend api',
      //   handler: async args => {
      //     args.deleteNodeService = MockApi.delNode
      //   },
      // }),
      hooks.addEdge.registerHook({
        name: 'get edge config from backend api',
        handler: async args => {
          // console.log('addEdge', args)
          // args.edgeConfig= {
          //   ...args.edgeConfig,
          //   label: 'Y'
          // }
          // args.edgeConfig = {
          //   ...args.edgeConfig,
          //   // zIndex: 0,
          //   attrs: {
          //     line: {
          //       stroke: 'red',
          //       targetMarker: {
          //         name: 'block',
          //         width: 12,
          //         height: 8,
          //       },
          //       strokeWidth: 1,
          //     },
          //   },
          // }
        }
      }),
      // hooks.delEdge.registerHook({
      //   name: 'get edge config from backend api',
      //   handler: async args => {
      //     args.deleteEdgeService = MockApi.delEdge
      //   },
      // }),
      hooks.updateNode.registerHook({
        name: 'update node',
        handler: async args => {
          console.log('update node', args)

          // args.updateNodeService =
        }
      })
    ]
    const toDispose = new DisposableCollection()
    toDispose.pushAll(list)
    return toDispose
  })
})
